#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/13 11:26
# @Author  : 王凯
# @File    : shaanxi_illegal.py
# @Project : spider-man

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import urlencode


class Shanxi1IllegalSpider(BaseTaxIllegalSpider):
    name = "shanxi_illegal"
    province: str = "山西"
    url: str = "http://shanxi.chinatax.gov.cn/topic/zdsswf/sx-11400"  # 取首页
    cookies = None

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.shanxi.chinatax.gov.cn:8443"})
        yield self.Request(
            self.url,
            callback=self.save_cookie,
            meta={"dont_redirect": True, "handle_httpstatus_list": [302]},
        )

    def save_cookie(self, response, **kwargs):
        cookies = (
            dict([response.headers.to_unicode_dict().get("Set-Cookie").split(";")[0].split("=")])
            if response.headers.to_unicode_dict().get("Set-Cookie")
            else {}
        )
        self.cookies = cookies
        url = "http://shanxi.chinatax.gov.cn/common/extQuery"
        for i in []:
            params = {
                "sqlid": "web_zdsswf",
                "limit": "10",
                "cx_lx": "0",
                "NSRMC": i,
                "NSRSBH": "",
                "ZZJGDM": "",
                "FDDBRXM": "",
                "CWRYXM": "",
                "CWRYSFZHM": "",
                "FDDBRSFZHM": "",
                "ZCDZ": "",
                "page": "1",
            }
            yield self.FormRequest(
                url + "?" + urlencode(params), formdata={"start": "0"}, callback=self.parse, cookies=self.cookies
            )

    def parse(self, response, **kwargs):
        yield from self.parse_list(response, **kwargs)
        page_num = response.json()["message"]["totalPage"]
        if page_num:
            for page in range(1, page_num + 1):
                url = "http://shanxi.chinatax.gov.cn/common/extQuery"
                params = {
                    "sqlid": "web_zdsswf",
                    "limit": "10",
                    "cx_lx": "0",
                    "NSRMC": "",
                    "NSRSBH": "",
                    "ZZJGDM": "",
                    "FDDBRXM": "",
                    "CWRYXM": "",
                    "CWRYSFZHM": "",
                    "FDDBRSFZHM": "",
                    "ZCDZ": "",
                    "page": f"{page}",
                }
                yield self.FormRequest(
                    url + "?" + urlencode(params),
                    formdata={"start": "0"},
                    callback=self.parse_list,
                    cookies=self.cookies,
                )

    def parse_list(self, response, **kwargs):
        print(response.text)
        datalist = response.json()["message"]["list"]
        for i in datalist:
            ajbh = i["ajbh"]
            url = "http://shanxi.chinatax.gov.cn/common/commonQueryOneData"
            data = {"sqlid": "web_ajxq", "ajbh": f"{ajbh}"}
            yield self.FormRequest(url, formdata=data, method="POST", callback=self.parse_detail)

    def parse_detail(self, response, **kwargs):
        data_dict = response.json().get("message", {})
        mapping_dict = {
            "NSRMC": "company_name",
            "ZCDZ": "address",
            "NSRSBH": "taxpayer_id",
            "ZZJGDM": "org_code",
            "FDDBRXX": "legal_representative",
            "AJXZ": "illegal_status",
            "ZYWFSS": "illegal_facts",
            "FLYJ_CLJG": "basis_and_punishment",
        }
        item = NetTaxIllegalProItem()
        item.province = self.province
        for data in mapping_dict:
            setattr(item, mapping_dict[data], data_dict.get(data))
        item.year = data_dict["XSRQ"].replace("-", "")
        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "shanxi_illegal"])
